package com.xymtop.sqlcater.core.sql.sqlexecute.select.entity;


import lombok.Data;

//连接对象
@Data
public class SqlJoin {

    //是否为主连接
    private Boolean isMain;

    //连接的集合
    private SelectCollect selectCollect1;

    //连接的集合2
    private SelectCollect selectCollect2;

    //连接的类型
    private SqlJoinType sqlJoinType;

    //连接的条件
    private   SqlWhere sqlWhere;


    public String buildSql(){
        String joinStr = "";
        if(selectCollect2!=null){


             joinStr = " JOIN ";
            if(sqlJoinType==SqlJoinType.LEFT){
                joinStr = "LEFT JOIN";
            }else if(sqlJoinType==SqlJoinType.FULL){
                joinStr = "FULL JOIN";
            }else if(sqlJoinType==SqlJoinType.RIGTH) {
                joinStr="RIGHT JOIN";
            }

            if(!isMain){
                joinStr="INNER JOIN";
            }

            if(selectCollect2.getSelectCollectType()==SelectCollectType.SQLQUERY){
                joinStr += "@"+selectCollect2.getName();
            }else {
                joinStr += selectCollect2.getName();
            }


            //连接的条件
            if(sqlWhere!=null){
                String whereSql = sqlWhere.buildSql();

                if(whereSql!=null&&whereSql!=""){
                    joinStr += " ON (" + whereSql+ ")";
                }

            }


        }

        return joinStr;
    }
}
